package com.chuanyekeji.aokai.Util;

import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统一生成返回代码
 */
public class ReturnData {
    /**
     * 返回成功的json代码
     * @return
     */
    public static String success(String msg) {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("code",1);
        data.put("msg",msg);
        return gson.toJson(data);
    }

    /**
     * 返回成功的json代码 带参数
     * @param msg
     * @param list
     * @return
     */
    public static String success(String msg, List<Object> list) {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("code",1);
        data.put("msg",msg);
        data.put("data",list);
        return gson.toJson(data);
    }

    /**
     * 返回成功的json代码 带参数
     * @param msg
     * @param obj
     * @return
     */
    public static String success(String msg, Object obj) {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("code",1);
        data.put("msg",msg);
        data.put("data",obj);
        return gson.toJson(data);
    }

    /**
     * 返回错误代码
     * @param code
     * @param msg
     * @return
     */
    public static String error(int code,String msg) {
        Gson gson = new Gson();
        Map<String,Object> data = new HashMap<>();
        data.put("code",code);
        data.put("msg",msg);
        return gson.toJson(data);
    }

    public static void main(String[] args) {
        JSONObject obj = new JSONObject("{resultCode:103414,desc:参数校验异常}");
        System.out.println(obj);
        Integer str = (Integer)obj.get("resultCode");
        System.out.println(str);
        String str1 = (String)obj.get("desc");
        System.out.println(str1);
    }
}
